-- ======================================================================
--  Hell Watch Lua IO App
-- 
-- Copyright 2014 Hell-Prototypes
--
-- http://www.hellprototypes.com
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hwio =require('hwluaio')

module(..., package.seeall);

-- I2C pin index in firmware is ------------------------------------------------
--I2C_SCK_PIN	: 7
--I2C_SDA_PIN	: 5

-- I2C device address define ---------------------------------------------------
IIC_WR_ADDR = 0xA0
IIC_RD_ADDR = 0xA1

-- -----------------------------------------------------------------------------
DEBUG_MODE	= 	false

function WAIT_ETU()
	--hwio.usdelay(1)
	--hwio.msdelay(500)
end
-- -----------------------------------------------------------------------------
function io_init()

end

function device_addr(addr)
	IIC_WR_ADDR = addr
	IIC_RD_ADDR = addr + 1
end

function start()
	local ret = hwio.i2c_start()
	if DEBUG_MODE then
		print("FW_I2C: start() ret = " .. ret)
	end
	return ret
end

function wr_byte(ch)
	local ret = hwio.i2c_write(ch)
	if DEBUG_MODE then
		print("FW_I2C: wr_byte() ret = " .. ret)
	end
	if ret < 0 then
		print("wr_byte() ret = " .. ret)
		return false
	end 
	if ret > 0 then
		print("wr_byte() NAK")
		stop()
		return false
	end
	
	return true
end

function rd_byte_nak()
	local ret = hwio.i2c_read(0)
	if DEBUG_MODE then
		print("FW_I2C: rd_byte_nak() ret = " .. ret)
	end
	return ret
end

function rd_byte_ack()
	local ret = hwio.i2c_read(1)
	if DEBUG_MODE then
		print("FW_I2C: rd_byte_ack() ret = " .. ret)
	end
	return ret
end

function stop()
	local ret = hwio.i2c_stop(1)
	if DEBUG_MODE then
		print("FW_I2C: stop() ret = " .. ret)
	end
	return ret
end

function repeated_start()
	start()
end
-- -----------------------------------------------------------------------------
function write_reg(reg_addr, data)
	start()
	
	if not wr_byte(IIC_WR_ADDR) then
		return false
	end

	if not wr_byte(reg_addr) then
		return false
	end

	if not wr_byte(data) then
		return false
	end

	stop()
	return true
end

function write_word_addr_reg(reg_addr, data)
	start()
	
	if not wr_byte(IIC_WR_ADDR) then
		return false
	end
	local H_Byte
	H_Byte = math.floor(reg_addr/256)
	-- print("H = " .. H_Byte .. ", L = " .. reg_addr - H_Byte*256 .. ", data = ", data)
	
	if not wr_byte(H_Byte) then
		return false
	end
	
	
	if not wr_byte(reg_addr - H_Byte*256) then
		return false
	end
	
	
	if not wr_byte(data) then
		return false
	end
	
	stop()
	return true
end

function read_reg(reg_addr)
	local ret = 0

	start()
	
	if not wr_byte(IIC_WR_ADDR) then
		return ret
	end
	
	if not wr_byte(reg_addr) then
		return ret
	end

	repeated_start()

	if not wr_byte(IIC_RD_ADDR) then
		return ret
	end

	ret = rd_byte_nak()

	stop()

	return ret
end
function read_regs(start_reg_addr, reg_num)
	local ret_table = {}
	local ret = 0

	if reg_num < 1 then
		return ret_table
	end
	start()

	if not wr_byte(IIC_WR_ADDR) then
		return ret_table
	end

	if not wr_byte(start_reg_addr) then
		return ret_table
	end

	repeated_start()

	if not wr_byte(IIC_RD_ADDR) then
		return ret_table
	end

	for i=1, reg_num do
		if i < reg_num then
			ret_table[i]  = rd_byte_ack()
		else
			ret_table[i]  = rd_byte_nak()
		end
	end

	stop()

	return ret_table
end
function read_word_addr_regs(start_reg_addr, reg_num)
	local ret_table = {}
	local ret = 0

	if reg_num < 1 then
		return ret_table
	end
	start()

	if not wr_byte(IIC_WR_ADDR) then
		return ret_table
	end

	local H_Byte
	H_Byte = math.floor(start_reg_addr/256)
	if not wr_byte(H_Byte) then
		return ret_table
	end

	
	if not wr_byte(start_reg_addr - H_Byte*256) then
		return false
	end

	repeated_start()

	if not wr_byte(IIC_RD_ADDR) then
		return ret_table
	end
	for i=1, reg_num do
		if i < reg_num then
			ret_table[i]  = rd_byte_ack()
		else
			ret_table[i]  = rd_byte_nak()
		end
	end

	stop()

	return ret_table
end
-- -----------------------------------------------------------------------------
function device_open()
	local ret = hwio.usb_open()
	if ret < 0 then
		return false
	end

	return true
end

function device_close()
	local ret = hwio.usb_close()
	if ret < 0 then
		return false
	end
	
	return true
end